package com.example.project4;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;

import android.util.Log;

public class SongsManager {
	// SDCard Path
    final String MEDIA_PATH = new String("/sdcard/");
    static final int MD5_DIGEST_LENGTH = 16;

    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
 
    // Constructor
    public SongsManager(){
 
    }
 
    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);
 
        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());
 
                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }
    
    public int getNumClienSongs(){
    	int numOfSongs = 0;
    	File home = new File(MEDIA_PATH);
    	 
        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                numOfSongs++;
            }
        }
        // return songs list array
        return numOfSongs;
    }
    
    public Song[] getSongs(){
    	int numOfSongs = 0;
    	File home = new File(MEDIA_PATH);
    	 
        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                numOfSongs++;
            }
        }
        Log.d("SongsManager: ", "number of songs in sdcard "+ numOfSongs);
        Song [] clientSongs = new Song[numOfSongs];
        
        String songTitle = "";
        int songSize = 0;
        
        if (home.listFiles(new FileExtensionFilter()).length > 0) {
        	int i = 0;
            for (File file : home.listFiles(new FileExtensionFilter())) {
            	songTitle = file.getName();
            	songSize = (int)file.length();
            	//MD5 Stuff
            	MessageDigest md;
            	byte[] digest = new byte[songSize];
            	byte[] mdbytes = new byte[MD5_DIGEST_LENGTH];
            	FileInputStream fis=null;
				try {
					md = MessageDigest.getInstance("MD5");					
					try {
						fis = new FileInputStream(file.getPath());
						
						int nread = 0; 
		                while ((nread = fis.read(digest)) != -1) {
		                  md.update(digest, 0, nread);
		                };
		                mdbytes = md.digest();				
					} catch (FileNotFoundException e) {
						Log.e("SongsManager: ", "Failed to read file...");
					} catch (IOException e) {
						Log.e("SongsManager: ", "Failed to read file...");
					}            	
				} catch (NoSuchAlgorithmException e) {
					Log.e("SongsManager: ", "Failed to MD5 file...");
				}
				clientSongs[i] = new Song (songTitle, mdbytes, songSize);
				i++;
            }
        }
    	
    	return clientSongs;
    }
 
    /**
     * Class to filter files which are having .mp3 extension
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }
    
}
